<?php
	include("DAO.php");
        include_once(OBJECT_PATH . "/Product.php");

	class ProductDAO extends DAO
	{
        /*
		function getProductList()
		{
            $xmlRequest =   '<client action="getall" object="product">' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            $products = new ArrayObject();
            foreach($xml->children() as $xmlProduct)
            {
                $product = new Product();
                $components = array();
                foreach($xmlProduct->children() as $productVar)
                {
                    if ($productVar->getName() == "id")
                        $product->setId($productVar);
                    else if ($productVar->getName() == "name")
                        $product->setName($productVar);
                    else if ($productVar->getName() == "description")
                        $product->setDescription($productVar);
                    else if ($productVar->getName() == "price")
                        $product->setPrice($productVar);
                    else if ($productVar->getName() == "component")
                    {
                        foreach($productVar->children() as $componentVar)
                        {
                            if ($componentVar->getName() == "id");
                                $components[] = $componentVar;
                        }
                    }
                }

                $product->setComponentIdList($components);
                $products->append($product);

            }

            var_dump($products);
            return $products;
		}
         */

		function getProductList()
		{
            $xmlRequest =   '<client action="getphplist" object="product">' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            $products = new ArrayObject();
            foreach($xml->children() as $xmlProduct)
            {
                $product = new Product();
                foreach($xmlProduct->children() as $productVar)
                {
                    if ($productVar->getName() == 'id')
                        $product->setId($productVar);
                    else if ($productVar->getName() == 'name')
                        $product->setName($productVar);
                }
                $products->append($product);

            }

            return $products;
		}

        function getProduct($id)
		{
            $xmlRequest =   '<client action="get" object="product">' .
                            '<product>' .
                                '<id>' . $id . '</id>' .
                            '</product>' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            foreach($xml->children() as $xmlProduct)
            {
                $product = new Product();
                $components = array();
                foreach($xmlProduct->children() as $productVar)
                {
                    if ($productVar->getName() == "id")
                        $product->setId($productVar);
                    else if ($productVar->getName() == "name")
                        $product->setName($productVar);
                    else if ($productVar->getName() == "description")
                        $product->setDescription($productVar);
                    else if ($productVar->getName() == "price")
                        $product->setPrice($productVar);
                    else if ($productVar->getName() == "component")
                    {
                        foreach($productVar->children() as $componentVar)
                        {
                            if ($componentVar->getName() == 'id')
                                $components[] = $componentVar;
                        }
                    }
                }

                $product->setComponentIdList($components);
                return $product;
            }

            // if this point is reached then an error has occured
            return null;
		}
		
		function addProduct($product)
		{
            $xmlRequest =   '<client action="add" object="product">' .
                            '<product>' .
                            '<id>-1</id>' . 
                            '<name>' . $product->getName() . '</name>' .
                            '<description>' . $product->getDescription() . '</description>' .
                            '<price>' . $product->getPrice() . '</price>';
            
            $components = $product->getComponentIdList();
            for ($i = 0; $i < sizeof($components); $i++)
            {
                $xmlRequest .=  '<component>' .
                                    '<id>' . $components[$i] . '</id>' .
                                '</component>';
            }

            $xmlRequest .=  '</product>' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            foreach($xml[0]->attributes() as $attribute => $value)
            {
                if ($attribute == "object")
                    if ($value == "failed")
                        return false;
                    else
                        return true;
            }
		}

		function editProduct($product)
		{
            $xmlRequest =   '<client action="edit" object="product">' .
                            '<product>' .
                            '<id>' . $product->getId() . '</id>' .
                            '<name>' . $product->getName() . '</name>' .
                            '<description>' . $product->getDescription() . '</description>' .
                            '<price>' . $product->getPrice() . '</price>';

            $components = $product->getComponentIdList();
            for ($i = 0; $i < sizeof($components); $i++)
            {
                $xmlRequest .=  '<component>' .
                                    '<id>' . $components[$i] . '</id>' .
                                    '<name></name>' .
                                    '<description></description>' .
                                    '<price></price>' .
                                    '<stock></stock>' .
                                    '<category></category>' .
                                '</component>';
            }
            $xmlRequest .= '</product>' .
            $xmlRequest .= '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            foreach($xml[0]->attributes() as $attribute => $value)
            {
                if ($attribute == "object")
                    if ($value == "failed")
                        return false;
                    else
                        return true;
            }
		}

		function deleteProduct($product)
		{
            $xmlRequest =   '<client action="delete" object="product">' .
                            '<product><id>' . $product->getId() . '</id></product>' .
                            '</client>';

            // get the response and sent it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            foreach($xml[0]->attributes() as $attribute => $value)
            {
                if ($attribute == "object")
                    if ($value == "failed")
                        return false;
                    else
                        return true;
            }
		}

        function getProductComponents($productId)
        {


            $xmlRequest =   '<client action="get" object="product">' .
                            '<product><id>' . $productId . '</id></product>' .
                            '</client>';

            // get the response and send it to an xml object
            $xmlResponse = $this->sendRequest($xmlRequest);
            $xml = simplexml_load_string($xmlResponse);

            $components = array();
            foreach ($xml->children() as $serverElement)
            {
                if ($serverElement->getName() == "component")
                {
                    foreach($serverElement->children() as $componentVar)
                    {
                        if ($componentVar->getName() == "id")
                            $components[] = $componentVar;
                    }
                }
            }

            return $components;
        }
	}
?>
